package com.readbook.action;


import com.readbook.service.WhomeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/whome")
public class whome {
    @Resource
    private WhomeService whomeService;


    /**
     * @author 牟明艳
     * @comments 查询首页轮播图图片
     * @time 2019年8月16日15:01:02
     */
    @RequestMapping("getHomeTopImage.html")
    @ResponseBody
    public List<Map<String, Object>> getHomeTopImage(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("code", "lunbo");
        List<Map<String, Object>> list1 = whomeService.getHomeTopImage(rmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 首页显示的内容
     * @time 2019年8月22日10:27:34
     */
    @RequestMapping("getHomeData.html")
    @ResponseBody
    public Map<String, Object> getHomeData(HttpServletRequest request, HttpServletResponse response) {
        //获取首页轮播图
        Map<String, Object> sylb = new HashMap<String, Object>();
        sylb.put("code", "lunbo");
        List<Map<String, Object>> tops = whomeService.getHomeTopImage(sylb);

        String startn = "0";//起始
        String endn = "20";//结束
        //教师推荐
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn);
        jstjmap.put("end", endn);
        jstjmap.put("tjly", "0");
        jstjmap.put("sh", "1");
        List<Map<String, Object>> jstj = whomeService.findBookPage(jstjmap);

        //家长推荐
        Map<String, Object> jztjmap = new HashMap<String, Object>();
        jztjmap.put("start", startn);
        jztjmap.put("end", endn);
        jztjmap.put("tjly", "1");
        jztjmap.put("sh", "1");
        List<Map<String, Object>> jztj = whomeService.findBookPage(jztjmap);

        //诗词天地
        Map<String, Object> scmap = new HashMap<String, Object>();
        scmap.put("start", startn);
        scmap.put("end", endn);
        List<Map<String, Object>> sctd = whomeService.findPoetryReadPage(scmap);

        //作品原创
        Map<String, Object> zpycmap = new HashMap<String, Object>();
        zpycmap.put("start", startn);
        zpycmap.put("end", endn);
        zpycmap.put("bstate", 2);

        List<Map<String, Object>> zpyc = whomeService.findBookxsycPage(zpycmap);

        Map<String, Object> homemap = new HashMap<String, Object>();
        homemap.put("tops", tops);
        homemap.put("jstj", jstj);
        homemap.put("jztj", jztj);
        homemap.put("sctd", sctd);
        homemap.put("zpyc", zpyc);
        return homemap;
    }


    /**
     * @author 牟明艳
     * @comments 教师推荐
     * @time 2019年8月22日16:46:24
     */
    @RequestMapping("getJstjData.html")
    @ResponseBody
    public Map<String, Object> getJstjData(HttpServletRequest request, HttpServletResponse response) {
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj");
        }
        String keyword = "";
        if (request.getParameter("keyword") != null) {
            keyword = request.getParameter("keyword");
        }

        String tjly = "";
        if (request.getParameter("tjly") != null) {
            tjly = request.getParameter("tjly");
        }

        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        //教师推荐
        Map<String, Object> jstjmap = new HashMap<String, Object>();
        jstjmap.put("start", startn + "");
        jstjmap.put("end", endn + "");
        jstjmap.put("tjly", tjly);
        jstjmap.put("sh", "1");
        if (!nj.equals("0")) {
            jstjmap.put("nj", nj);
        }
        jstjmap.put("keyword", keyword);

        List<Map<String, Object>> jstj = whomeService.findBookPage(jstjmap);
        int totalnum = whomeService.countBookPage(jstjmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", jstj);
        rmap.put("totalpage", totalpage);
        return rmap;
    }


}
